home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '87 / Source ƒ.sea / Source ƒ / communications source ƒ / macbinary source / macbin.c next >
Encoding:
C/C++ Source or Header  |  1985-10-27  |  5.0 KB  |  251 lines  |  [TEXT/????]

  1. #include "stdio.h"
  2. #include "macCdefs.h"
  3. #include "Window.h"
  4. #include "Packages.h"
  5. #include "PBDefs.h"
  6. #include "OSIo.h"
  7.  
  8.  
  9. struct F_BLK{ // note everything is char to avoid alignment padding
  10.     char zero1;
  11.     char nlen;
  12.     char name[63];
  13.     char type[4];
  14.     char creator[4];
  15.     char flags;
  16.     char zero2;
  17.     char location[6];
  18.     char protected;
  19.     char zero3;
  20.     char dflen[4];
  21.     char rflen[4];
  22.     char cdate[4];
  23.     char mdate[4];
  24. };
  25.  
  26. struct F_BLK INFO_BLOCK,*F_INFO;
  27.  
  28. char *fname,*srcname;
  29. char sector[128],BINARY,THISFORK;
  30. short secnum,vrefnum,infile,outfile;
  31. short Rblocks,Dblocks,BLOCKS;
  32. long dlen,rlen;
  33. SFTypeList types;
  34. SFReply RP;
  35. FileParam FP;
  36.  
  37. _GetFileInfo(vol,name,iop)
  38. short vol;
  39. char *name;
  40. FileParam *iop;
  41. {    CtoPstr((char *)iop->ioNamePtr = (char *)strsave(name));
  42.     iop->ioVRefNum=vol;
  43.     iop->ioFVersNum=iop->ioFDirIndex=0;
  44.     PBGetFInfo(iop,0);
  45.     free(iop->ioNamePtr);
  46. }
  47.  
  48. _SetFileInfo(vol,name,iop)
  49. short vol;
  50. char *name;
  51. FileParam *iop;
  52. {    CtoPstr((char *)iop->ioNamePtr = (char *)strsave(name));
  53.     iop->ioVRefNum=vol;
  54.     iop->ioFVersNum=iop->ioFDirIndex=0;
  55.     PBSetFInfo(iop,0);
  56.     free(iop->ioNamePtr);
  57. }
  58.  
  59. MakeTextFile(vol,name,iop)
  60. short vol;
  61. char *name;
  62. FileParam *iop;
  63. {    _GetFileInfo(vol,name,iop);
  64.     iop->ioFlFndrInfo.fdType='TEXT';
  65.     iop->ioFlFndrInfo.fdCreator='EDIT';
  66.     _SetFileInfo(vol,name,iop);
  67. }
  68.  
  69. _pack(f,t)
  70. char *f,*t;
  71. {    BlockMove(f,t,4);
  72. }
  73.  
  74. _open(file,acmode)
  75. char *file,acmode;
  76. {    return(TKopen(diskdevice,file,acmode));
  77. }
  78.  
  79. _read(file,sector,size)
  80. {    clearbytes(sector,size);
  81.     return(read(file,sector,size));
  82. }
  83.     
  84. static int NumOfBlks(num)
  85. long num;
  86. {    int blks=num/128;
  87.     if(num%128) ++blks;
  88.     return(blks);
  89. }
  90.  
  91. static char IsBinary(f)
  92. struct F_BLK *f;
  93. {    BINARY=((f->zero1 == 0) && (f->zero2 == 0) && (f->zero3 == 0));
  94. }
  95.  
  96. static WriteBlk()
  97. {    if(secnum == 0)
  98.     {    IsBinary(sector);
  99.         if(BINARY) InitBinSave();
  100.         else signal("Not a Binary File.");
  101.         return
  102.     }
  103.     WriteBinary();
  104. }
  105.  
  106. static InitBinSave()
  107. {    BlockMove(sector,&INFO_BLOCK,sizeof(INFO_BLOCK));
  108.     _pack(&INFO_BLOCK.rflen[0],&rlen);
  109.     _pack(&INFO_BLOCK.dflen[0],&dlen);
  110.     Rblocks=NumOfBlks(rlen);
  111.     Dblocks=NumOfBlks(dlen);
  112.     if(Dblocks != 0)
  113.     {    THISFORK = 0;
  114.         BLOCKS=Dblocks;
  115.     }
  116.     else
  117.     {    THISFORK = 0xf0;
  118.         BLOCKS=Rblocks;
  119.     }
  120.     if(!NewFileName(&INFO_BLOCK.nlen,&RP)) signal("Request Aborted.");
  121.     vrefnum=MakeName(&RP,fname);
  122.     if(OSFind(fname)) OSDelete(fname);
  123.     outfile=_open(fname,THISFORK+2);
  124. }
  125.  
  126.  
  127. static _truncate(len)
  128. long len;
  129. {    setbytepos(outfile,len);
  130.     seteof(outfile);
  131. }
  132.  
  133. static WriteBinary()
  134. {    if(BLOCKS)
  135.     {    write(outfile,sector,128);
  136.         --BLOCKS;
  137.     }
  138.     if(BLOCKS) return;
  139.     if(THISFORK == 0)
  140.     {    BLOCKS=Rblocks;
  141.         THISFORK = 0xf0;
  142.         _truncate(dlen);
  143.         TKclose(outfile);
  144.         if(BLOCKS) outfile=_open(fname,0xf2);
  145.         else outfile=0;
  146.     }
  147.     else _truncate(rlen);
  148. }
  149.  
  150. static FinishFile()
  151. {
  152.     _GetFileInfo(vrefnum,fname,&FP);
  153.     BlockMove(&(INFO_BLOCK.type[0]),&(FP.ioFlFndrInfo),sizeof(FInfo));
  154.     FP.ioFlFndrInfo.fdFlags &= 0xfeff; // clr init bit
  155.     _pack(&(INFO_BLOCK.cdate[0]),&FP.ioFlCrDat);
  156.     _pack(&(INFO_BLOCK.mdate[0]),&FP.ioFlMdDat);
  157.     _SetFileInfo(vrefnum,fname,&FP);
  158. }
  159.  
  160.  
  161. App2Bin()
  162. {    if(!OldFileName(-1,0,&RP)) return;
  163.     MakeName(&RP,srcname);
  164.     clearbytes(F_INFO=(struct F_BLK *)sector,128);
  165.     BlockMove(&RP.Namelength,fname,64);
  166.     BlockMove(&RP.Namelength,&F_INFO->nlen,64);
  167.     PtoCstr(fname);
  168.     strcat(fname,".bin");
  169.     CtoPstr(fname);
  170.     if(!NewFileName(fname,&RP)) return;
  171.     vrefnum=MakeName(&RP,fname);
  172.     if(OSFind(fname)) OSDelete(fname);
  173.     if((infile=_open(srcname,1)) == 0)
  174.         signal("Unable to read the file.");
  175.     if((outfile=_open(fname,2)) == 0)
  176.         signal("Unable to create the file.");
  177.     _GetFileInfo(vrefnum,srcname,&FP);
  178.     BlockMove(&FP.ioFlFndrInfo,&(F_INFO->type[0]),sizeof(FInfo));
  179.     F_INFO->protected = (F_INFO->zero2 & 0x40)?1:0;
  180.     F_INFO->zero2=0;
  181.     _pack(&FP.ioFlLgLen,&(F_INFO->dflen[0]));
  182.     _pack(&FP.ioFlRLgLen,&(F_INFO->rflen[0]));
  183.     _pack(&FP.ioFlCrDat,&(F_INFO->cdate[0]));
  184.     _pack(&FP.ioFlMdDat,&(F_INFO->mdate[0]));
  185.     write(outfile,sector,128);
  186.     while(_read(infile,sector,128)>0) write(outfile,sector,128);
  187.     TKclose(infile);
  188.     infile=_open(srcname,0xf1); // Open the resource fork now
  189.     while(_read(infile,sector,128)>0) write(outfile,sector,128);
  190.     TKclose(infile);
  191.     TKclose(outfile);
  192.     MakeTextFile(vrefnum,fname,&FP);
  193. }
  194.  
  195. Bin2App()
  196. {    types.ftype[0]='TEXT';
  197.     if(!OldFileName(1,&types,&RP)) return;
  198.     MakeName(&RP,srcname);
  199.     if((infile=_open(srcname,1)) == 0) signal("Unable to read the file.");
  200.     secnum=0;
  201.     while(_read(infile,sector,128))
  202.     {    WriteBlk();
  203.         ++secnum;
  204.     }
  205.     TKclose(infile);
  206.     TKclose(outfile);
  207.     FinishFile();
  208. }
  209.  
  210. DoDialog(id,msg)
  211. short id;
  212. char *msg;
  213. {    char *oldPort,*dialog,*msgp;
  214.     short Hit;
  215.     GetPort(&oldPort);
  216.     SetPort(dialog=GetNewDialog(id,0,-1));
  217.     if(msg)
  218.     {    MoveTo(20,10);
  219.         DrawString(msgp=strsave(msg));
  220.         free(msgp);
  221.     }
  222.     ModalDialog(0,&Hit);
  223.     DisposeDialog(dialog);
  224.     SetPort(oldPort);
  225.     return(Hit);
  226. }
  227.  
  228. main()
  229. {    char *msg,*CatchSignal();
  230.     InitDialogs(0);
  231.     InitCursor();
  232.     fname=malloc(256);
  233.     srcname=malloc(256);
  234.     if(msg=CatchSignal())
  235.     {    DoDialog(1,msg);
  236.     }
  237. Again:
  238.     switch(DoDialog(2,0))
  239.     {    case 2: App2Bin(); break;
  240.         case 3: Bin2App(); break;
  241.         case 4: 
  242.         {    free(fname);
  243.             free(srcname);
  244.             exit();
  245.         }
  246.     }
  247.     goto Again;
  248. }
  249.  
  250.     
  251.